<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\UserExpert;
use App\Repos\UserExpertRepo;

class ExpertController extends Controller
{
    public function index(Request $request)
    {
        $where = [];
        $list  = UserExpert::where($where)->paginate(20);        
        
        return view('admin.expert.index',compact('list','worktypes'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.expert.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');
        //dd($data);
        $rs   = UserExpertRepo::save($data);
        if($rs) { 
           
            return redirect('zadmin/experts');
        }
        return back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = UserExpert::find($id);     
      
        return view('admin.expert.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except('_token','_method');
        $where['id'] = $id;
       
        // 文件是否上传成功
        if ($request->hasFile('avatar')) {
            $file = $request->avatar;
            $ext      = $file->getClientOriginalExtension();    
            $saveName = time().rand().".".$ext;

            $file->storeAs('public/'.date('Ymd'), $saveName);
            $path = '/storage/'.date('Ymd').'/'.$saveName;     
            $data['avatar'] = $path;         
        }

       
        // 文件是否上传成功
        if ($request->hasFile('banner')) {
            $banner   = $request->banner;
            $ext      = $banner->getClientOriginalExtension();    
            $saveName = time().rand().".".$ext;

            $banner->storeAs('public/'.date('Ymd'), $saveName);
            $path = '/storage/'.date('Ymd').'/'.$saveName;     
            $data['banner'] = $path;         
        }
        $rs   = UserExpertRepo::update($where,$data);
        if($rs) {
            return redirect('zadmin/experts');
        }
        return back()->withInput();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $rs = UserExpert::destroy($id);
        if ($rs) {
            return redirect('zadmin/experts');
        }
        return back();
    }
}
